/**
 * @file: proc.c
 * @author: yumeng (imyumeng@qq.com)
 * @brief:
 * @version 0.1
 * @date: 2021-09-06
 *
 * @copyright Meng_Yu Copyright (c) 2021
 *
 * @history: init first version. 2021-09-06
 */
#include <stdio.h>
#include <fcntl.h>
#include <stdio.h>
#include <fcntl.h>
#include <windows.h>
#include <commctrl.h>
#include <assert.h>
#include "common.h"
#include "resource.h"
#include "proc.h"

extern struct proc_data g_data;

#ifdef DISP_LOG_EN
void InitConsoleWindow(void)
{
	int nCrt = 0;
	FILE* fp;
	AllocConsole();
	nCrt = _open_osfhandle((intptr_t)GetStdHandle(STD_OUTPUT_HANDLE), _O_TEXT);
	fp = _fdopen(nCrt, "w");
	*stdout = *fp;
	setvbuf(stdout, NULL, _IONBF, 0);
}

void PrintError(unsigned int dwErrcode)
{
	LPVOID lpMsgBuf;
	FormatMessage(
	    FORMAT_MESSAGE_ALLOCATE_BUFFER |
	    FORMAT_MESSAGE_FROM_SYSTEM |
	    FORMAT_MESSAGE_IGNORE_INSERTS,
	    NULL, dwErrcode,
	    MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
	    (LPTSTR) &lpMsgBuf,
	    0, NULL );
	printf((LPCSTR)lpMsgBuf);
	LocalFree(lpMsgBuf);
}
#endif

void Read_Tnifile(void)
{
	char cs[32] = {0};
	char *ce = (char *)'\0';
	//g_data.StayOnTop = GetPrivateProfileInt(INI_SECTION_BINCALC, INI_KEY_STAYONTOP, 0, INI_NAME);
	g_data.mode_32bit = GetPrivateProfileInt(INI_SECTION_BINCALC, INI_KEY_32BIT_MODE, 0, INI_NAME);
	g_data.init_focus = GetPrivateProfileInt(INI_SECTION_BINCALC, INI_KEY_INIT_FOCUS, 0, INI_NAME);
	g_data.dec_signed = GetPrivateProfileInt(INI_SECTION_BINCALC, INI_KEY_SIGNED, 0, INI_NAME);
	g_data.dec_float = GetPrivateProfileInt(INI_SECTION_BINCALC, INI_KEY_FLOAT, 0, INI_NAME);
	g_data.language = GetPrivateProfileInt(INI_SECTION_BINCALC, INI_KEY_LANG, 0, INI_NAME);
	//g_data.OffsetX = GetPrivateProfileInt(INI_SECTION_BINCALC, INI_KEY_OFST_X, 200, INI_NAME);
	//g_data.OffsetY = GetPrivateProfileInt(INI_SECTION_BINCALC, INI_KEY_OFST_Y, 200, INI_NAME);
	GetPrivateProfileString(INI_SECTION_BINCALC, INI_KEY_DATA, INI_DEF_STR, (LPTSTR)cs, 80, INI_NAME);
	g_data.Data = _strtoui64(cs, &ce, 16);
}

void Write_Tnifile(void)
{
	char tmp_str[32] = {0};
	//_ui64toa(g_data.Data, cs, 16)
	//sprintf(tmp_str, "%u", g_data.StayOnTop);
	//WritePrivateProfileString(INI_SECTION_BINCALC, INI_KEY_STAYONTOP, tmp_str, INI_NAME);
	sprintf(tmp_str, "%u", g_data.dec_signed);
	WritePrivateProfileString(INI_SECTION_BINCALC, INI_KEY_SIGNED, tmp_str, INI_NAME);
	sprintf(tmp_str, "%u", g_data.dec_float);
	WritePrivateProfileString(INI_SECTION_BINCALC, INI_KEY_FLOAT, tmp_str, INI_NAME);
	sprintf(tmp_str, "%u", g_data.mode_32bit);
	WritePrivateProfileString(INI_SECTION_BINCALC, INI_KEY_32BIT_MODE, tmp_str, INI_NAME);
	sprintf(tmp_str, "%u", g_data.init_focus);
	WritePrivateProfileString(INI_SECTION_BINCALC, INI_KEY_INIT_FOCUS, tmp_str, INI_NAME);
	sprintf(tmp_str, "%u", g_data.language);
	WritePrivateProfileString(INI_SECTION_BINCALC, INI_KEY_LANG, tmp_str, INI_NAME);
	//g_data.OffsetX = g_data.OffsetX>1800?1800:g_data.OffsetX;
	//sprintf(tmp_str, "%d", g_data.OffsetX);
	//WritePrivateProfileString(INI_SECTION_BINCALC, INI_KEY_OFST_X, tmp_str, INI_NAME);
	//g_data.OffsetY = g_data.OffsetY>800?800:g_data.OffsetY;
	//sprintf(tmp_str, "%d", g_data.OffsetY);
	WritePrivateProfileString(INI_SECTION_BINCALC, INI_KEY_OFST_Y, tmp_str, INI_NAME);
	sprintf(tmp_str, "%I64X", g_data.Data);
	WritePrivateProfileString(INI_SECTION_BINCALC, INI_KEY_DATA, tmp_str, INI_NAME);
}

void data2ui(void)
{
	static uint8_t flag = 1;
	static uint64_t data_bk = 0;
	uint64_t disp_data;
	char tmp_str[65] = {0};
	HWND hwnd;

	// LOG_DBUG("@@\r\n");
	if (g_data.mode_32bit)
		disp_data = (uint32_t)g_data.Data;
	else
		disp_data = g_data.Data;

	hwnd = GetDlgItem(g_data.hwndDlg, g_data.init_focus ? IDC_EDT_DEC : IDC_EDT_HEX);
	if (hwnd != GetFocus())
		SetFocus(hwnd);

	for (qword_t i = 0; i < 64; i++)
	{
		if (flag || ((data_bk & QWORD_BIT(i)) != (disp_data & QWORD_BIT(i))))
		{
			SendDlgItemMessage(g_data.hwndDlg, IDC_STC_BIT0 + i, STM_SETIMAGE, (WPARAM)IMAGE_BITMAP, 
				(LPARAM)g_data.bin_bmp[(QWORD_BIT(i) == (disp_data & QWORD_BIT(i))) ? 1 :0 ]);
		}
	}
	sprintf(tmp_str, "%I64X", disp_data); //"%I64X"
	SetDlgItemText(g_data.hwndDlg, IDC_EDT_HEX, tmp_str);

	if (g_data.dec_signed)
	{
		if (g_data.dec_float)
		{
			if (g_data.mode_32bit)
				sprintf(tmp_str, "%g", *(float *)&disp_data); //*(float *)
			else
				sprintf(tmp_str, "%lg", *(double *)&disp_data); //(double)1.1);
		}
		else
		{
			if (g_data.mode_32bit)
				sprintf(tmp_str, "%d", (uint32_t)disp_data);
			else
				sprintf(tmp_str, "%I64d", (uint64_t)disp_data);
		}
	}
	else
	{
		sprintf(tmp_str, "%I64u", disp_data);
	}

	SetDlgItemText(g_data.hwndDlg, IDC_EDT_DEC, tmp_str);

	memset((void *)tmp_str, 0, 32);
	int temp = (disp_data >> 60) ? 60 : (disp_data >> 50) ? 50
	           : (disp_data >> 40)   ? 40
	           : (disp_data >> 30)   ? 30
	           : (disp_data >> 20)   ? 20
	           : (disp_data >> 10)   ? 10
	           : 0;
	switch (temp)
	{
		case 60:
			sprintf(tmp_str, "%I64uEiB", disp_data >> temp);
			break;
		case 50:
			sprintf(tmp_str, "%I64uPiB", disp_data >> temp);
			break;
		case 40:
			sprintf(tmp_str, "%I64uTiB", disp_data >> temp);
			break;
		case 30:
			sprintf(tmp_str, "%I64uGiB", disp_data >> temp);
			break;
		case 20:
			sprintf(tmp_str, "%I64uMiB", disp_data >> temp);
			break;
		case 10:
			sprintf(tmp_str, "%I64uKiB", disp_data >> temp);
			break;
		default:
			sprintf(tmp_str, "%I64uB", disp_data >> temp);
			break;
	}
	SetDlgItemText(g_data.hwndDlg, IDC_STC_CAP, tmp_str);

	if (g_data.calc_flag)
	{
		if (!g_data.calc_equ)
		{
			g_data.dat2 = disp_data;
			sprintf(g_data.disp_str, g_data.hex_view ? "0x%I64X%c0x%I64X" : "%I64u%c%I64u", g_data.dat1, g_data.calc_flag, g_data.dat2);
		}
		else
		{
			sprintf(g_data.disp_str, g_data.hex_view ? "0x%I64X%c0x%I64X=" : "%I64u%c%I64u=", g_data.dat1, g_data.calc_flag, g_data.dat2);
		}
	}
	else
	{
		sprintf(g_data.disp_str, g_data.hex_view ? "0x%I64X" : "%I64u", disp_data);
	}
	SetDlgItemText(g_data.hwndDlg, IDC_EDT_DISP, g_data.disp_str);

	data_bk = disp_data;
	flag = 0;
}

LONG_PTR CALLBACK edit_proc_hex(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	char len;
	char cs[32] = {0};
	char cpoy_str[32] = {0};
	char *ce = (char *)'\0';
	POINT pt;
	HMENU sub;
	HMENU menu;
	qword_t bit_msk;

	// LOG_DBUG("uMsg:%d\r\n",uMsg);
	switch (uMsg)
	{
		// case WM_LBUTTONDBLCLK: //double clack select all
		//     return TRUE;
		case EM_SETSEL:
			return TRUE;
		case WM_NCPAINT:
		{
			// SendMessage(hwnd, EM_SETSEL, -2, -1);
			// HideCaret(hwnd);
			DestroyCaret();
			break;
		}
		case WM_SETFOCUS:
			g_data.init_focus = focus_hex;
			GetDlgItemText(g_data.hwndDlg, IDC_EDT_DEC, cpoy_str, 32);
			SetDlgItemText(g_data.hwndDlg, IDC_EDT_DEC, cpoy_str);
			break;
		// case WM_KILLFOCUS:
		//     break;
		case WM_CONTEXTMENU:
		{
			menu = LoadMenu(g_data.hThisApp, MAKEINTRESOURCE(IDR_MENU_HEX));
			sub = GetSubMenu(menu, 0);

			// GetWindowText(hwnd,cs,32);
			SendMessage(hwnd, WM_GETTEXT, 32, (LPARAM)cs);
			sprintf(cpoy_str, "Copy As \"0x%s\" (C-Style)", cs);
			ModifyMenu(sub, 2, MF_BYPOSITION, IDM_MENU_COPY_C, cpoy_str);
			sprintf(cpoy_str, "Copy As \"%s\" (Pure Number)", cs);
			ModifyMenu(sub, 3, MF_BYPOSITION, IDM_MENU_COPY_P, cpoy_str);
			GetCursorPos(&pt);
			TrackPopupMenu(sub, TPM_LEFTBUTTON | TPM_TOPALIGN | TPM_RIGHTBUTTON,
			               pt.x, pt.y, 0, hwnd, NULL);
			DestroyMenu(menu);
			return TRUE;
		}
		case WM_COMMAND:
		{
			word_t wParaL = LOWORD(wParam);
			if ((wParaL != IDM_MENU_PASTE) &&
			    (wParaL != IDM_MENU_COPY_C) &&
			    (wParaL != IDM_MENU_COPY_P))
				return TRUE;
			OpenClipboard(hwnd);
			SendMessage(hwnd, WM_GETTEXT, 32, (LPARAM)cs);
			switch (wParaL)
			{
				case IDM_MENU_PASTE:
				{
					if (IsClipboardFormatAvailable(CF_TEXT))
					{
						HGLOBAL hg = GetClipboardData(CF_TEXT);
						LPSTR lp_str = (LPSTR)GlobalLock(hg);
						g_data.Data = _strtoui64(lp_str, &ce, 16);
						data2ui();
					}
				}
				break;
				case IDM_MENU_COPY_C:
				{
					EmptyClipboard();
					sprintf(cpoy_str, "0x%s", cs);
					len = strlen(cpoy_str) + 1;
					HGLOBAL hg = GlobalAlloc(GMEM_MOVEABLE, len);
					LPSTR lp_str = (LPSTR)GlobalLock(hg);
					memcpy(lp_str, cpoy_str, len);
					GlobalUnlock(hg);
					SetClipboardData(CF_TEXT, lp_str);
				}
				break;
				case IDM_MENU_COPY_P:
				{
					EmptyClipboard();
					len = strlen(cs) + 1;
					HGLOBAL hg = GlobalAlloc(GMEM_MOVEABLE, len);
					LPSTR lp_str = (LPSTR)GlobalLock(hg);
					memcpy(lp_str, cs, len);
					GlobalUnlock(hg);
					SetClipboardData(CF_TEXT, lp_str);
				}
				break;

				default:
					break;
			}
			CloseClipboard();
			return TRUE;
		}
		case WM_CHAR:
			if (LOWORD(wParam) == BS)
			{
				g_data.Data = (g_data.Data >> 4);
				data2ui();
			}
			else if ((LOWORD(wParam) == '+') || (LOWORD(wParam) == '-') || (LOWORD(wParam) == '*') || (LOWORD(wParam) == '/') ||
			         (LOWORD(wParam) == '%') || (LOWORD(wParam) == '^') || (LOWORD(wParam) == '|') || (LOWORD(wParam) == '&') ||
			         (LOWORD(wParam) == 'L') || (LOWORD(wParam) == 'R') || (LOWORD(wParam) == 'l') || (LOWORD(wParam) == 'r'))
			{
				g_data.calc_flag = LOWORD(wParam);
				if (g_data.calc_flag == 'l')
					g_data.calc_flag = 'L';
				if (g_data.calc_flag == 'r')
					g_data.calc_flag = 'R';
				g_data.dat1 = g_data.Data;
				g_data.dat2 = 0;
				g_data.Data = 0;
				data2ui();
			}
			else if (LOWORD(wParam) == '=')
			{
				g_data.dat2 = g_data.Data;
				switch (g_data.calc_flag)
				{
					case '+':
						g_data.Data = g_data.dat1 + g_data.dat2;
						break;
					case '-':
						g_data.Data = g_data.dat1 - g_data.dat2;
						break;
					case '*':
						g_data.Data = g_data.dat1 * g_data.dat2;
						break;
					case '/':
						if (g_data.dat2)
							g_data.Data = g_data.dat1 / g_data.dat2;
						break;
					case '%':
						g_data.Data = g_data.dat1 % g_data.dat2;
						break;
					case '^':
						g_data.Data = g_data.dat1 ^ g_data.dat2;
						break;
					case '|':
						g_data.Data = g_data.dat1 | g_data.dat2;
						break;
					case '&':
						g_data.Data = g_data.dat1 & g_data.dat2;
						break;
					case 'L':
						// if (g_data.dat2 < QWORD_BITS)
						g_data.Data = g_data.dat1 << g_data.dat2;
						break;
					case 'R':
						g_data.Data = g_data.dat1 >> g_data.dat2;
						break;
					default:
						break;
				}
				g_data.calc_equ = 1;
				data2ui();
				g_data.calc_flag = 0;
				g_data.calc_equ = 0;
			}
			else if (LOWORD(wParam) == 'h' || LOWORD(wParam) == 'H')
			{
				g_data.hex_view = !g_data.hex_view;
				SendDlgItemMessage(g_data.hwndDlg, IDC_CHK_HEX_VIEW, BM_SETCHECK, g_data.hex_view, 0);
				data2ui();
			}
			else if (HEXCHK(LOWORD(wParam)))
			{
				bit_msk = g_data.mode_32bit ? DWORD_SHL(0xF, 28) : QWORD_SHL(0xF, 60);
				if (!(g_data.Data & bit_msk))
				{
					g_data.Data = (g_data.Data << 4) + GETNUM(LOWORD(wParam));
					data2ui();
				}
			}
			break;
		case WM_KEYDOWN:
		{
			qword_t bit_msk;
			switch (wParam)
			{
				case VK_LEFT:
					// LOG_DBUG("VK_LEFT\r\n");
					bit_msk = g_data.mode_32bit ? DWORD_BIT(31) : QWORD_BIT(63);
					if (!(g_data.Data & bit_msk))
					{
						g_data.Data = (g_data.Data << 1);
						data2ui();
					}
					return TRUE;
				case VK_RIGHT:
					// LOG_DBUG("VK_RIGHT\r\n");
					if (g_data.Data)
					{
						g_data.Data = (g_data.Data >> 1);
						data2ui();
					}
					return TRUE;
				default:
					break;
			}
			break;
		}
	}
	return CallWindowProc((WNDPROC)g_data.old_hex_edit_proc, hwnd, uMsg, wParam, lParam);
}

LONG_PTR CALLBACK edit_proc_dec(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	char len;
	char cs[32] = {0};
	char cpoy_str[32] = {0};
	char *ce = (char *)'\0';
	POINT pt;
	HMENU sub;
	HMENU menu;
	qword_t bit_msk;

	switch (uMsg)
	{
		case EM_SETSEL:
			return TRUE;
		case WM_NCPAINT:
			DestroyCaret();
			break;
		case WM_SETFOCUS:
			g_data.init_focus = focus_dec;
			GetDlgItemText(g_data.hwndDlg, IDC_EDT_HEX, cpoy_str, 32);
			SetDlgItemText(g_data.hwndDlg, IDC_EDT_HEX, cpoy_str);
			break;
		case WM_CONTEXTMENU:
		{
			menu = LoadMenu(g_data.hThisApp, MAKEINTRESOURCE(IDR_MENU_DEC));
			sub = GetSubMenu(menu, 0);

			SendMessage(hwnd, WM_GETTEXT, 32, (LPARAM)cs);
			sprintf(cpoy_str, "Copy As \"%s\" (Pure Number)", cs);
			ModifyMenu(sub, 2, MF_BYPOSITION, IDM_MENU_COPY_P, cpoy_str);
			GetCursorPos(&pt);
			TrackPopupMenu(sub, TPM_LEFTBUTTON | TPM_TOPALIGN | TPM_RIGHTBUTTON,
			               pt.x, pt.y, 0, hwnd, NULL);
			DestroyMenu(menu);
		}
		return TRUE;
		case WM_COMMAND:
		{
			switch (LOWORD(wParam))
			{
				case IDM_MENU_PASTE:
					OpenClipboard(hwnd);
					SendMessage(hwnd, WM_GETTEXT, 32, (LPARAM)cs);
					if (IsClipboardFormatAvailable(CF_TEXT))
					{
						HGLOBAL hg = GetClipboardData(CF_TEXT);
						LPSTR lp_str = (LPSTR)GlobalLock(hg);
						g_data.Data = _strtoui64(lp_str, &ce, 10);
						data2ui();
					}
					CloseClipboard();
					break;
				case IDM_MENU_COPY_P:
				{
					OpenClipboard(hwnd);
					SendMessage(hwnd, WM_GETTEXT, 32, (LPARAM)cs);
					EmptyClipboard();
					len = strlen(cs) + 1;
					HGLOBAL hg = GlobalAlloc(GMEM_MOVEABLE, len);
					LPSTR lp_str = (LPSTR)GlobalLock(hg);
					memcpy(lp_str, cs, len);
					GlobalUnlock(hg);
					SetClipboardData(CF_TEXT, lp_str);
					CloseClipboard();
				}
				break;
				default:
					break;
			}
		}
		return TRUE;
		case WM_CHAR:
			if (LOWORD(wParam) == BS)
			{
				if (g_data.dec_signed)
					g_data.Data = ((int64_t)g_data.Data / 10);
				else
					g_data.Data = (g_data.Data / 10);
				data2ui();
			}
			// else if (LOWORD(wParam) == '-')
			// {
			//     if (g_data.dec_signed)
			//     {
			//         int64_t temp = (int64_t)g_data.Data;
			//         temp = -temp;
			//         g_data.Data = temp;
			//         data2ui();
			//     }
			// }
			else if ((LOWORD(wParam) == '+') || (LOWORD(wParam) == '-') || (LOWORD(wParam) == '*') || (LOWORD(wParam) == '/') ||
			         (LOWORD(wParam) == '%') || (LOWORD(wParam) == '^') || (LOWORD(wParam) == '|') || (LOWORD(wParam) == '&') ||
			         (LOWORD(wParam) == 'L') || (LOWORD(wParam) == 'R') || (LOWORD(wParam) == 'l') || (LOWORD(wParam) == 'r'))
			{
				g_data.calc_flag = LOWORD(wParam);
				if (g_data.calc_flag == 'l')
					g_data.calc_flag = 'L';
				if (g_data.calc_flag == 'r')
					g_data.calc_flag = 'R';
				g_data.dat1 = g_data.Data;
				g_data.dat2 = 0;
				g_data.Data = 0;
				data2ui();
			}
			else if (LOWORD(wParam) == '=')
			{
				g_data.dat2 = g_data.Data;
				switch (g_data.calc_flag)
				{
					case '+':
						g_data.Data = g_data.dat1 + g_data.dat2;
						break;
					case '-':
						g_data.Data = g_data.dat1 - g_data.dat2;
						break;
					case '*':
						g_data.Data = g_data.dat1 * g_data.dat2;
						break;
					case '/':
						if (g_data.dat2)
							g_data.Data = g_data.dat1 / g_data.dat2;
						break;
					case '%':
						g_data.Data = g_data.dat1 % g_data.dat2;
						break;
					case '^':
						g_data.Data = g_data.dat1 ^ g_data.dat2;
						break;
					case '|':
						g_data.Data = g_data.dat1 | g_data.dat2;
						break;
					case '&':
						g_data.Data = g_data.dat1 & g_data.dat2;
						break;
					case 'L':
						// if (g_data.dat2 < QWORD_BITS)
						g_data.Data = g_data.dat1 << g_data.dat2;
						break;
					case 'R':
						g_data.Data = g_data.dat1 >> g_data.dat2;
						break;
					default:
						break;
				}
				g_data.calc_equ = 1;
				data2ui();
				g_data.calc_flag = 0;
				g_data.calc_equ = 0;
			}
			else if (LOWORD(wParam) == 'h' || LOWORD(wParam) == 'H')
			{
				g_data.hex_view = !g_data.hex_view;
				SendDlgItemMessage(g_data.hwndDlg, IDC_CHK_HEX_VIEW, BM_SETCHECK, g_data.hex_view, 0);
				data2ui();
			}
			else if (DECCHK(LOWORD(wParam)))
			{
				int64_t int_tmp, int_max, int_min;
				if (g_data.mode_32bit)
				{
					int_max = INT32_MAX;
					int_min = INT32_MIN;
					bit_msk = DWORD_MASK;
				}
				else
				{
					int_max = INT64_MAX;
					int_min = INT64_MIN;
					bit_msk = QWORD_MASK;
				}
				if (g_data.dec_signed)
				{
					int_tmp = (int64_t)g_data.Data;
					if (int_tmp >= 0)
					{
						if ((int_tmp * 10) < (int_max / 10))
							g_data.Data = (int_tmp * 10) + GETNUM(LOWORD(wParam));
					}
					else
					{
						if ((int_tmp * 10) > (int_min / 10))
							g_data.Data = (int_tmp * 10) - GETNUM(LOWORD(wParam));
					}
				}
				else
				{
					if ((g_data.Data * 10) < (bit_msk / 10))
						g_data.Data = (g_data.Data * 10) + GETNUM(LOWORD(wParam));
				}
				data2ui();
			}
			break;
		case WM_KEYDOWN:
		{
			qword_t bit_msk;
			switch (wParam)
			{
				case VK_LEFT:
					// LOG_DBUG("VK_LEFT\r\n");
					bit_msk = g_data.mode_32bit ? DWORD_BIT(31) : QWORD_BIT(63);
					if (!(g_data.Data & bit_msk))
					{
						g_data.Data = (g_data.Data << 1);
						data2ui();
					}
					return TRUE;
				case VK_RIGHT:
					// LOG_DBUG("VK_RIGHT\r\n");
					if (g_data.Data)
					{
						g_data.Data = (g_data.Data >> 1);
						data2ui();
					}
					return TRUE;
				default:
					break;
			}
			break;
		}
	}
	return CallWindowProc((WNDPROC)g_data.old_dec_edit_proc, hwnd, uMsg, wParam, lParam);
}

LONG_PTR CALLBACK edit_proc_disp(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	if (uMsg == WM_CONTEXTMENU || uMsg == EM_SETSEL) //|| uMsg == WM_LBUTTONDBLCLK
		return TRUE;
	if (uMsg == WM_NCPAINT)
	{
		DestroyCaret();
	}
	return CallWindowProc((WNDPROC)g_data.old_disp_edit_proc, hwnd, uMsg, wParam, lParam);
}

LONG_PTR CALLBACK btn_proc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	if ((uMsg == BM_SETSTYLE) || (uMsg == WM_SETFOCUS))
		return TRUE;
	return CallWindowProc((WNDPROC)g_data.old_btn_proc, hwnd, uMsg, wParam, lParam);
}
